//
//  WBStatusPictureView.swift
//  simpleWeibo
//
//  Created by sunmumu on 2018/10/28.
//  Copyright © 2018 孙林. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {
    var viewModel : WBStatusViewModel? {
        didSet {
            calcViewSize()
            urls = viewModel?.picURLs
        }
    }
    
    // 根据视图模型的配图视图大小 调整显示内容
    private func calcViewSize() {
        //处理宽高
        //1 单图 根据配图视图的大小 修改subview[0] 的宽高
        if viewModel?.picURLs?.count == 1 {
            let viewSize = viewModel?.pictureViewSize ?? CGSize()
            //1) 获取第0个图像视图
            let view = subviews[0]
            view.frame = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: viewSize.width, height: viewSize.height)
        } else {
            //2 多图(无图)  恢复subview[0] 的宽高 保证九宫格布局的完整
            //1) 获取第0个图像视图
            let view = subviews[0]
            view.frame = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: WBStatusPictureItemWidth, height: WBStatusPictureItemWidth - WBStatusPictureViewOutterMargin)
        }
        //修改高度约束
        heightConstraint.constant = viewModel?.pictureViewSize.height ?? 0
        
    }
    
    private var urls : [WBStatusPicture]? {
        didSet {
            //1隐藏所有的 imageView
            for v in subviews {
                v.isHidden = true
            }
            
            //2遍历urls 设置imageView 的url
            var index = 0
            for url in urls ?? [] {
                let pictureImageView = subviews[index] as! UIImageView
                
                //3 4张图形处理
                if index == 1 && urls?.count == 4 {
                    index += 1
                }
                
                pictureImageView.cz_setImage(urlString: url.thumbnail_pic, placeholder: nil, isAvatar: false)
                index += 1
                
                pictureImageView.isHidden = false
            }
        }
        
    }
    
    @IBOutlet weak var heightConstraint: NSLayoutConstraint!
    
    override func awakeFromNib() {
        setupUI()
    }

}

// MARK: - 设置视图
extension WBStatusPictureView {
    fileprivate func setupUI() {
        backgroundColor = superview?.backgroundColor
        //超出边界的内容 不显示
        clipsToBounds = true
        let count = 3
        let rect = CGRect(x: 0, y: WBStatusPictureViewOutterMargin, width: WBStatusPictureItemWidth, height: WBStatusPictureItemWidth)
        
        for i in 0..<count * count {
            let row = CGFloat(i / count)
            let column = CGFloat(i % count)
            let xOffset = column * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            let yOffset = row * (WBStatusPictureItemWidth + WBStatusPictureViewInnerMargin)
            
            let pictureImageView = UIImageView()
            pictureImageView.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            pictureImageView.contentMode = .scaleAspectFill
            pictureImageView.clipsToBounds = true
            
            addSubview(pictureImageView)
        }
        
    }
}
